/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ptr.ptrsystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import com.ptr.controladores.*;
import com.ptr.ptrsystem.dao.Lista;

/**
 *
 * @author WALDIR
 */
@WebServlet(name = "DetallesMatrimonioServlet", urlPatterns = {"/DetallesMatrimonio"})
public class DetallesMatrimonioServlet extends HttpServlet {

    public DetallesMatrimonioServlet() {
    }
    String NPERSONA1;
    String PAPERSONA1;
    String SAPERSONA1;
    
    String NPERSONA2;
    String PAPERSONA2;
    String SAPERSONA2;
    
    String NTESTIGO1;
    String PATESTIGO1;
    String SATESTIGO1;
    
    String NTESTIGO2;
    String PATESTIGO2;
    String SATESTIGO2;
    
    String NREGISTRADOR;
    String PAREGISTRADOR;
    String SAREGISTRADOR;
    String TiBu;
    conexionsqlserver con = new conexionsqlserver();
    Connection cn = null;
    Statement st = null;
    ResultSet rs = null;
    String QUERY = "";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        NPERSONA1 = request.getParameter("NP1");
        PAPERSONA1= request.getParameter("PAP1");
        SAPERSONA1= request.getParameter("SAP1");
        
        NPERSONA2 = request.getParameter("NP2");
        PAPERSONA2 = request.getParameter("PAP2");
        SAPERSONA2 = request.getParameter("SAP2");
        
        NTESTIGO1 = request.getParameter("NT1");
        PATESTIGO1 = request.getParameter("PAT1");
        SATESTIGO1 = request.getParameter("SAT1");
        
        NTESTIGO2 = request.getParameter("NT2");
        PATESTIGO2 = request.getParameter("PAT2");
        SATESTIGO2 = request.getParameter("SAT2");
                
        NREGISTRADOR = request.getParameter("NR");
        PAREGISTRADOR = request.getParameter("PAR");
        SAREGISTRADOR = request.getParameter("SAR");
       // String codigo=request.getParameter("codigo");
        
        TiBu = request.getParameter("btn1");

        if (TiBu.equals("XP1")) {
            busXnp1(request, response);
        }

        if (TiBu.equals("XP2")) {
            //  busXnp2(request, response);
        }

        if (TiBu.equals("XT1")) {
            //  busXnt1(request, response);
        }

        if (TiBu.equals("XT2")) {
            //  busXnt2(request, response);
        }

        if (TiBu.equals("XR")) {
            //  busXnr(request, response);
        }
        if (TiBu.equals("busqueda")) {
            busquedaGNRLxNombre(request, response);
        }
    }

    protected void busXnp1(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            con.conectar();
            cn = con.getconection();
            st = cn.createStatement();
            QUERY = "select m.* from TB_PERSONA p,TB_MATRIMONIO m where p.NRO_ACTA_PERS=m.NRO_ACTA_PERS and PRE_NOM_PERS like('%" + NPERSONA1 + "%')";
            rs = st.executeQuery(QUERY);

            while (rs.next()) {
                request.setAttribute("NAP", rs.getString("NRO_ACTA_PERS"));
                request.setAttribute("NAM", rs.getString("NRO_ACTA_MAT"));
                request.setAttribute("FRM", rs.getString("FECHA_REG_MAT"));
                request.setAttribute("UBIM", rs.getString("IDUBIGEO_MAT"));
                request.setAttribute("NP1", rs.getString("DNI_PERS_1"));
                request.setAttribute("NP2", rs.getString("DNI_PERS_2"));
                request.setAttribute("NT1", rs.getString("DNI_TESTIGO_1"));
                request.setAttribute("NT2", rs.getString("DNI_TESTIGO_2"));
                request.setAttribute("NR", rs.getString("DNI_REGISTRADOR"));
                request.setAttribute("EST", rs.getString("COD_ESTADO"));
                request.setAttribute("UBI", rs.getString("IDUBIGEO"));
                request.setAttribute("NAC", rs.getString("COD_NACIONALIDAD"));
                request.getRequestDispatcher("/RegCivil/jsp/D2Matrimonio.jsp");
            }
            con.cerrarBD();
            st.close();
            cn.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void busquedaGNRLxNombre(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            con.conectar();
            cn = con.getconection();
            st = cn.createStatement();
            QUERY = "select m.* from TB_PERSONA p,TB_MATRIMONIO m where p.NRO_ACTA_PERS=m.NRO_ACTA_PERS and P.PRE_NOM_PERS like('%" + NPERSONA1 + "%') and p.PRIM_APE_PERS like('%"+PAPERSONA1+"%') and P.SEG_APE_PERS like('%"+SAPERSONA1+"%')";
            rs = st.executeQuery(QUERY);
            out.println("<center>");
            out.println("<table>");
            out.println("<thead>");
            out.println("<tr>"
                    + "<th width='20' height='30' ></th>"
                    + "<th width='150' height='30' >NRO ACT MAT</th>"
                    + "<th width='150' height='30'>FECHA REG MAT</th>"
                    + "<th width='150' height='30'>NOMBRE PERSONA</th>"
                    + "<th width='150' height='30'>NOMBRE TESTIGO 1 </th>"
                    + "<th width='150' height='30'>NOMBRE TESTIGO 2 </th>"
                    + "<th width='150' height='30'>NOMBRE REGISTRADOR</th>"
                    + "</tr>");
            out.println("</thead><tbody>");
            while (rs.next()) {

                out.println("<tr><td align='center'><input type='radio' name='matrimonio' value='"+rs.getString("NRO_ACTA_MAT")+"' /></td>");
                //out.println("<td align='center'>"+rs.getString("NRO_ACTA_PERS")+"</td>");
                out.println("<td align='center'>" + rs.getString("NRO_ACTA_MAT") + "</td>");
                out.println("<td align='center'>" + rs.getString("FECHA_REG_MAT") + "</td>");
                //out.println(rs.getString("IDUBIGEO_MAT"));
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_PERS_1")) + "</td>");
                // out.println("<td align='center'>"+Lista.getNombrePersona(rs.getString("DNI_PERS_2"))+"</td></tr>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_TESTIGO_1")) + "</td>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_TESTIGO_2")) + "</td>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_REGISTRADOR")) + "</td></TR>");
                // out.println(Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                // out.println(rs.getString("IDUBIGEO"));
                // out.println(rs.getString("COD_NACIONALIDAD"));
            }
            out.println("<tbody></table>");
            out.println("</center>");
            con.cerrarBD();
            st.close();
            cn.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
//

    protected void busquedaGNRLxTestigo1(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            con.conectar();
            cn = con.getconection();
            st = cn.createStatement();
           QUERY = "select m.* from TB_PERSONA p,TB_MATRIMONIO m where p.NRO_ACTA_PERS=m.NRO_ACTA_PERS and P.PRE_NOM_PERS like('%" + NTESTIGO1 + "%') and p.PRIM_APE_PERS like('%"+PAPERSONA1+"%') and P.SEG_APE_PERS like('%"+SAPERSONA1+"%')";
            rs = st.executeQuery(QUERY);
            out.println("<center>");
            out.println("<table>");
            out.println("<thead>");
            out.println("<tr>"
                    + "<th width='20' height='30' ></th>"
                    + "<th width='150' height='30' >NRO ACT MAT</th>"
                    + "<th width='150' height='30'>FECHA REG MAT</th>"
                    + "<th width='150' height='30'>NOMBRE PERSONA</th>"
                    + "<th width='150' height='30'>NOMBRE TESTIGO 1 </th>"
                    + "<th width='150' height='30'>NOMBRE TESTIGO 2 </th>"
                    + "<th width='150' height='30'>NOMBRE REGISTRADOR</th>"
                    + "</tr>");
            out.println("</thead><tbody>");
            while (rs.next()) {

                out.println("<tr><td align='center'></td>");
                //out.println("<td align='center'>"+rs.getString("NRO_ACTA_PERS")+"</td>");
                out.println("<td align='center'>" + rs.getString("NRO_ACTA_MAT") + "</td>");
                out.println("<td align='center'>" + rs.getString("FECHA_REG_MAT") + "</td>");
                //out.println(rs.getString("IDUBIGEO_MAT"));
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_PERS_1")) + "</td>");
                // out.println("<td align='center'>"+Lista.getNombrePersona(rs.getString("DNI_PERS_2"))+"</td></tr>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_TESTIGO_1")) + "</td>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_TESTIGO_2")) + "</td>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_REGISTRADOR")) + "</td></TR>");
                // out.println(Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                // out.println(rs.getString("IDUBIGEO"));
                // out.println(rs.getString("COD_NACIONALIDAD"));
            }
            out.println("<tbody></table>");
            out.println("</center>");
            con.cerrarBD();
            st.close();
            cn.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //
     protected void busquedaGNRLxTestigo2(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            con.conectar();
            cn = con.getconection();
            st = cn.createStatement();
            QUERY = "select m.* from TB_PERSONA p,TB_MATRIMONIO m where p.NRO_ACTA_PERS=m.NRO_ACTA_PERS and DNI_TESTIGO_2 like('%" + NTESTIGO2 + "%')";
            rs = st.executeQuery(QUERY);
            out.println("<center>");
            out.println("<table>");
            out.println("<thead>");
            out.println("<tr>"
                    + "<th width='20' height='30' ></th>"
                    + "<th width='150' height='30' >NRO ACT MAT</th>"
                    + "<th width='150' height='30'>FECHA REG MAT</th>"
                    + "<th width='150' height='30'>NOMBRE PERSONA</th>"
                    + "<th width='150' height='30'>NOMBRE TESTIGO 1 </th>"
                    + "<th width='150' height='30'>NOMBRE TESTIGO 2 </th>"
                    + "<th width='150' height='30'>NOMBRE REGISTRADOR</th>"
                    + "</tr>");
            out.println("</thead><tbody>");
            while (rs.next()) {

                out.println("<tr><td align='center'></td>");
                //out.println("<td align='center'>"+rs.getString("NRO_ACTA_PERS")+"</td>");
                out.println("<td align='center'>" + rs.getString("NRO_ACTA_MAT") + "</td>");
                out.println("<td align='center'>" + rs.getString("FECHA_REG_MAT") + "</td>");
                //out.println(rs.getString("IDUBIGEO_MAT"));
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_PERS_1")) + "</td>");
                // out.println("<td align='center'>"+Lista.getNombrePersona(rs.getString("DNI_PERS_2"))+"</td></tr>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_TESTIGO_1")) + "</td>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_TESTIGO_2")) + "</td>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_REGISTRADOR")) + "</td></TR>");
                // out.println(Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                // out.println(rs.getString("IDUBIGEO"));
                // out.println(rs.getString("COD_NACIONALIDAD"));
            }
            out.println("<tbody></table>");
            out.println("</center>");
            con.cerrarBD();
            st.close();
            cn.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //
     protected void busquedaGNRLxRegistrador(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            con.conectar();
            cn = con.getconection();
            st = cn.createStatement();
            QUERY = "select m.* from TB_PERSONA p,TB_MATRIMONIO m where p.NRO_ACTA_PERS=m.NRO_ACTA_PERS and DNI_REGISTRADOR like('%" + NREGISTRADOR + "%')";
            rs = st.executeQuery(QUERY);
            out.println("<center>");
            out.println("<table>");
            out.println("<thead>");
            out.println("<tr>"
                    + "<th width='20' height='30' ></th>"
                    + "<th width='150' height='30' >NRO ACT MAT</th>"
                    + "<th width='150' height='30'>FECHA REG MAT</th>"
                    + "<th width='150' height='30'>NOMBRE PERSONA</th>"
                    + "<th width='150' height='30'>NOMBRE TESTIGO 1 </th>"
                    + "<th width='150' height='30'>NOMBRE TESTIGO 2 </th>"
                    + "<th width='150' height='30'>NOMBRE REGISTRADOR</th>"
                    + "</tr>");
            out.println("</thead><tbody>");
            while (rs.next()) {

                out.println("<tr><td align='center'></td>");
                //out.println("<td align='center'>"+rs.getString("NRO_ACTA_PERS")+"</td>");
                out.println("<td align='center'>" + rs.getString("NRO_ACTA_MAT") + "</td>");
                out.println("<td align='center'>" + rs.getString("FECHA_REG_MAT") + "</td>");
                //out.println(rs.getString("IDUBIGEO_MAT"));
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_PERS_1")) + "</td>");
                // out.println("<td align='center'>"+Lista.getNombrePersona(rs.getString("DNI_PERS_2"))+"</td></tr>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_TESTIGO_1")) + "</td>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_TESTIGO_2")) + "</td>");
                out.println("<td align='center'>" + Lista.getNombrePersona(rs.getString("DNI_REGISTRADOR")) + "</td></TR>");
                // out.println(Lista.getNombreEstado(rs.getString("COD_ESTADO")));
                // out.println(rs.getString("IDUBIGEO"));
                // out.println(rs.getString("COD_NACIONALIDAD"));
            }
            out.println("<tbody></table>");
            out.println("</center>");
            con.cerrarBD();
            st.close();
            cn.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
